package Sequence;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class Test0107 {
}

class Solution0107_1 {
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        LinkedList<List<Integer>> ans = new LinkedList<>();
        if (root == null) return ans;

        ArrayDeque<TreeNode> nodes = new ArrayDeque<>();
        nodes.add(root);
        while (!nodes.isEmpty()) {
            ArrayList<Integer> integers = new ArrayList<>();
            int size = nodes.size();
            for (int i = 0; i < size; i++) {
                TreeNode node = nodes.poll();
                integers.add(node.val);
                if (node.left!=null) nodes.add(node.left);
                if (node.right!=null) nodes.add(node.right);
            }

            ans.push(integers);
        }

        return ans;
    }
}